package com.dragon.www.haier.net;

import android.content.Context;

import com.dragon.www.haier.R;
import com.dragon.www.haier.entity.Device;
import com.dragon.www.haier.entity.DeviceInfo;
import com.dragon.www.haier.entity.HaierDevicesInfo;
import com.dragon.www.haier.entity.HaierDevicesStatusInfo;
import com.dragon.www.haier.entity.ServiceResult;
import com.dragon.www.haier.util.Config;
import com.google.gson.GsonBuilder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/8/24 0024.
 */
public class NetHelper {
//    public static String baseUrl = "http://192.168.1.109:8080/ZNJJ";
//    public static String baseUrl = "http://58.210.52.235:8080/ZNJJ/";
//    public static String baseactionUrl = "http://192.168.1.2:8981/hem/";
//    public static String baseactionUrl = "http://10.79.26.211:8981/hem/";

    /**
     * 用户注册
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void userRegister(Context context, String username, String password, final NetUtils.NetCallBack<ServiceResult> netCallBack, final Class<?> rspCls) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        NetUtils.post(context, context.getString(R.string.baseUrl)+ context.getString(R.string.registerUrl), map, "注册中...", netCallBack, rspCls);
    }

    /**
     * 用户登录
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void userLogin(Context context, String username, String password, final NetUtils.NetCallBack<ServiceResult> netCallBack, final Class<?> rspCls) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", username);
        map.put("password", password);
        NetUtils.post(context, context.getString(R.string.baseUrl) + context.getString(R.string.loginUrl), map, "登录中...", netCallBack, rspCls);
    }

    /**
     * 用户密码修改
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void userPasswordChange(Context context, String passwordOld, String passwordNew, final NetUtils.NetCallBack<ServiceResult> netCallBack, final Class<?> rspCls) {
        Map<String, Object> map = new HashMap<>();
        map.put("username", Config.getUserInfo().getUserName());
        map.put("oldpassword", passwordOld);
        map.put("newpassword", passwordNew);
        NetUtils.post(context, context.getString(R.string.baseUrl) + context.getString(R.string.updatePasswordUrl), map, "密码更改中...", netCallBack, rspCls);
    }

    /**
     * 获取房间
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void getRooms(Context context, final NetUtils.NetCallBack<ServiceResult> netCallBack, final Class<?> rspCls) {
        Map<String, Object> map = new HashMap<>();
        map.put("Content-Type", "application/json; charset=UTF-8");
        map.put("username", Config.getUserInfo().getUserName());
        NetUtils.post(context, context.getString(R.string.baseUrl) + context.getString(R.string.getRoomUrl), map, "获取房间中...", netCallBack, rspCls);
    }

    /**
     * 获取设备
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void getDevices(Context context, int roomid, final NetUtils.NetCallBack<ServiceResult> netCallBack, final Class<?> rspCls){
        Map<String, Object> map = new HashMap<>();
        map.put("roomid", String.valueOf(roomid));
        NetUtils.post(context, context.getString(R.string.baseUrl) + context.getString(R.string.getEquipUrl), map, "正在获取房间设备列表...", netCallBack, rspCls);
    }

    /**
     * 更新场景列表
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void updateScene(Context context, List<Map<String, Object>> roomlists, final NetUtils.NetCallBack<ServiceResult> netCallBack, final Class<?> rspCls){
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> data = new HashMap<>();
        data.put("data",roomlists);
        map.put("username", Config.getUserInfo().getUserName());
        map.put("content", new GsonBuilder().create().toJson(data).toString());
        NetUtils.post(context, context.getString(R.string.baseUrl)+ context.getString(R.string.updateSceneUrl), map, "正在更新场景设备列表...", netCallBack, rspCls);
    }

    /**
     * 从网络提取场景列表
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void getScene(Context context, final NetUtils.NetCallBack<ServiceResult> netCallBack, final Class<?> rspCls){
        Map<String, Object> map = new HashMap<>();
        map.put("username", Config.getUserInfo().getUserName());

        NetUtils.post(context, context.getString(R.string.baseUrl) + context.getString(R.string.getSceneUrl), map, "正在更新场景设备列表...", netCallBack, rspCls);
    }

    /**
     * 发送单条控制指令(继电器)
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void sendAction(Context context, int boxid, int sourceid, int deviceid, String action, int value, String type, final NetUtils.NetCallBack<DeviceInfo> netCallBack, final Class<?> rspCls){
        JSONObject params1 = new JSONObject();
        JSONObject params = new JSONObject();
        JSONObject JsonObject = new JSONObject();
        try {
            params1.put("deviceid", deviceid);
            params1.put("action", action);
            params1.put("value", value);
            params.put(type,  params1);
            params.put("boxid", boxid);
            params.put("sourceid", sourceid);
            JsonObject.put("version", "1.0");
            JsonObject.put("halaction",params);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        NetUtils.post(context, context.getString(R.string.baseUrl)+ context.getString(R.string.udpUrl), JsonObject, "", netCallBack, rspCls);
    }

    /**
     * 发送单条控制指令(空调、窗帘)
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void sendAction(Context context, int boxid, int sourceid, String action, int value, String type, final NetUtils.NetCallBack<DeviceInfo> netCallBack, final Class<?> rspCls){

        JSONObject params1 = new JSONObject();
        JSONObject params = new JSONObject();
        JSONObject JsonObject = new JSONObject();

        try {
            params1.put("action", action);
            params1.put("value", value);
            params.put(type,  params1);
            params.put("boxid", boxid);
            params.put("sourceid", sourceid);
            JsonObject.put("version", "1.0");
            JsonObject.put("halaction",params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        NetUtils.post(context, context.getString(R.string.baseUrl)+ context.getString(R.string.udpUrl), JsonObject, "", netCallBack, rspCls);
    }

    /**
     * 按类型获取设备状态
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void getTypeDevices(Context context, int roomid, String type, final NetUtils.NetCallBack<DeviceInfo> netCallBack, final Class<?> rspCls){

        JSONObject params = new JSONObject();
        JSONObject JsonObject = new JSONObject();

        try {
            params.put("type", type);
            params.put("roomid", roomid);
            JsonObject.put("version", "1.0");
            JsonObject.put("halstatus",params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        NetUtils.post(context, context.getString(R.string.baseUrl)+ context.getString(R.string.udpUrl), JsonObject, "", netCallBack, rspCls);
    }
    /**
     * 情景模式
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void sendscene(Context context, int boxid,int box, int sourceid, List<Integer> air ,int curtainvalue,List<int[]> lamp,List<Device> lamps,int windsystemDeviceValue,int humidifierDeviceValue,final NetUtils.NetCallBack<DeviceInfo> netCallBack, final Class<?> rspCls){

        JSONObject params = new JSONObject();
        JSONObject JsonObject = new JSONObject();
        JSONObject airparams1 = new JSONObject();
        JSONObject airparams2 = new JSONObject();
        JSONObject airparams3 = new JSONObject();
        JSONObject airparams4 = new JSONObject();
        JSONObject curtainparams = new JSONObject();
        JSONObject airparams5 = new JSONObject();
        JSONArray airja = new JSONArray();
        JSONArray lampja = new JSONArray();

        try {
            airparams1.put("action", "power");
            airparams1.put("value", air.get(0));
            airparams2.put("action", "mode");
            airparams2.put("value", air.get(1));
            airparams3.put("action", "speed");
            airparams3.put("value", air.get(2));
            airparams4.put("action", "temperature");
            airparams4.put("value", air.get(3));

            airja.put(airparams1);
            airja.put(airparams2);
            airja.put(airparams3);
            airja.put(airparams4);
            airparams5.put("actiongroup",airja);

            for (int i=0;i<lamps.size();i++){
                JSONObject lampparams = new JSONObject();
                lampparams.put("deviceid", Integer.parseInt(lamps.get(i).getequipNo().split("-")[1]));
                lampparams.put("value", lamp.get(box)[Integer.parseInt(lamps.get(i).getequipNo().split("-")[1])-1]);
                lampparams.put("action", "power");
                lampja.put(lampparams);
            }
            JSONObject humidifierparams = new JSONObject();
            humidifierparams.put("deviceid", 6);
            humidifierparams.put("value", humidifierDeviceValue);
            humidifierparams.put("action", "power");
            lampja.put(humidifierparams);
            JSONObject windsystemparams = new JSONObject();
            windsystemparams.put("deviceid", 5);
            windsystemparams.put("value", windsystemDeviceValue);
            windsystemparams.put("action", "power");
            lampja.put(windsystemparams);

            curtainparams.put("action", "start");
            curtainparams.put("value", curtainvalue);
            params.put("boxid", boxid);
            params.put("relay", lampja);
            params.put("curtain", curtainparams);
            params.put("aircondition", airparams5);
            params.put("sourceid", sourceid);
            JsonObject.put("version", "1.0");
            JsonObject.put("halsetting", params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        NetUtils.post(context, context.getString(R.string.baseUrl)+ context.getString(R.string.udpUrl), JsonObject, "", netCallBack, rspCls);
    }
    /**
     * 全开全关
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void sendall(Context context, String roomid,int sourceid, int value, final NetUtils.NetCallBack<DeviceInfo> netCallBack, final Class<?> rspCls){

        JSONObject params1 = new JSONObject();
        JSONObject params = new JSONObject();
        JSONObject JsonObject = new JSONObject();
        try {
            params1.put("action", "power");
            params1.put("value", value);
            params.put("all",  params1);
            params.put("roomid", roomid);
            params.put("sourceid", sourceid);
            JsonObject.put("version", "1.0");
            JsonObject.put("halaction",params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        NetUtils.post(context,context.getString(R.string.baseUrl)+ context.getString(R.string.udpUrl), JsonObject, "", netCallBack, rspCls);
    }
    /**
     * 获取海尔设备状态
     *
     * @param context
     * @param netCallBack
     */
    public static void getHaierDevicesStatus(Context context, int masterId, int slaveId,int devicetype,int commtype, final NetUtils.NetCallBack<HaierDevicesStatusInfo> netCallBack){
        JSONObject params = new JSONObject();
        JSONObject JsonObject = new JSONObject();
        try {
            params.put("masterId", masterId);
            params.put("slaveId", slaveId);
            params.put("devicetype", devicetype);
            params.put("commtype", commtype);
            JsonObject.put("version", "1.0");
            JsonObject.put("halteststatus",params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        NetUtils.post(context,context.getString(R.string.haierbaseUrl), JsonObject, "", netCallBack);
    }
    /**
     * 控制海尔设备
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void ControlHaierDevices(Context context, int masterId, int slaveId, int devicetype,int commtype,List<Integer> controldata, final NetUtils.NetCallBack<DeviceInfo> netCallBack, final Class<?> rspCls){
        JSONObject params = new JSONObject();
        JSONObject JsonObject = new JSONObject();
        JSONArray params1 = new JSONArray();
        try {
            for (int i= 0;i < controldata.size(); i++){
                params1.put(controldata.get(i));
            }
            params.put("masterId", masterId);
            params.put("slaveId", slaveId);
            params.put("devicetype", devicetype);
            params.put("commtype", commtype);
            params.put("controldata", params1);
            JsonObject.put("version", "1.0");
            JsonObject.put("haltest",params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        NetUtils.post(context,context.getString(R.string.haierbaseUrl), JsonObject, "", netCallBack, rspCls);
    }
    /**
     * 获取海尔设备
     *
     * @param context
     * @param netCallBack
     * @param rspCls
     */
    public static void getHaierDevices(Context context, final NetUtils.NetCallBack<HaierDevicesInfo> netCallBack, final Class<?> rspCls){

        JSONObject JsonObject = new JSONObject();
        JSONObject params = new JSONObject();
        try {
            JsonObject.put("version", "1.0");
            JsonObject.put("haltestdevices",params);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        NetUtils.post(context,context.getString(R.string.haierbaseUrl), JsonObject, netCallBack, rspCls);
    }
}
